package com.evildog.workspace.life.application.controller;

import com.evildog.workspace.common.object.PageQuery;
import com.evildog.workspace.common.result.PagedResult;
import com.evildog.workspace.common.result.Result;
import com.evildog.workspace.common.result.ResultGenerator;
import com.evildog.workspace.dependencies.mybatis.util.QueryConditionUtils;
import com.evildog.workspace.life.application.bo.DiaryBO;
import com.evildog.workspace.life.application.bo.DiaryPageBO;
import com.evildog.workspace.life.application.convert.DiaryConvert;
import com.evildog.workspace.life.application.model.DiaryPageParam;
import com.evildog.workspace.life.application.model.DiaryParam;
import com.evildog.workspace.life.application.service.DiaryService;
import com.evildog.workspace.life.application.vo.DiaryPageVO;
import com.evildog.workspace.life.application.vo.DiaryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * diary Controller
    日记
 * </p>
 *
 * @author evildog
 * @since 2020-08-04
 */
@RestController
@RequestMapping("/diaries")
@AllArgsConstructor
@Api(tags = "diary相关接口")
public class DiaryController {

    private DiaryService diaryService;

    @PostMapping
    @ApiOperation(value = "添加接口")
    public Result<Boolean> save(@RequestBody DiaryParam diaryParam) {
        return ResultGenerator.status(diaryService.save(DiaryConvert.INSTANCE.convert(diaryParam)));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public Result<Boolean> removeById(@PathVariable("id") Long id) {
        return ResultGenerator.status(diaryService.removeById(id));
    }

    @DeleteMapping("/batch-remove")
    @ApiOperation(value = "批量删除接口")
    public Result<Boolean> removeByIds(@RequestParam("ids") List<Integer> ids) {
        return ResultGenerator.status(diaryService.removeByIds(ids));
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "更新接口")
    public Result<Boolean> updateById(@PathVariable("id") Long id,@RequestBody DiaryParam diaryParam) {
        return ResultGenerator.status(diaryService.updateById(id, DiaryConvert.INSTANCE.convert(diaryParam)));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询详情接口")
    public Result<DiaryVO> getById(@PathVariable("id") Long id) {
        DiaryBO diaryBO = diaryService.getDetailById(id);
        return ResultGenerator.success(DiaryConvert.INSTANCE.convert(diaryBO));
    }

    @GetMapping("/page")
    @ApiOperation(value = "表分页查询接口")
    public Result<PagedResult<DiaryPageVO>> getPageList(DiaryPageParam diaryPageParam, PageQuery query){
        PagedResult<DiaryPageBO> diaryPageBO = diaryService.getPageList(
                        query,
                        QueryConditionUtils.getQueryCondition(diaryPageParam),
                        DiaryConvert.INSTANCE.convert(diaryPageParam));
        return ResultGenerator.success(DiaryConvert.INSTANCE.convert(diaryPageBO));
    }


}